package com.lenovo.smartengine.utils;

import com.dolby.api.DsAccessException;
import com.dolby.api.DsGlobalEx;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

public class AudioTypeSetUtils {
	public static int queryAudioTypeFromWaves(Context mContext){
		int media_type = -1;
		int enable = -1;
		ContentResolver contentResolver = mContext.getContentResolver();
		Cursor cursor = contentResolver.query(Constants.CONFIG_URI, new String[]{"media_type","enable"}, null, null, null);
		if(cursor!=null){
			cursor.moveToFirst();
			String mediatypestr = cursor.getString(cursor.getColumnIndex("media_type"));
			String enablestr = cursor.getString(cursor.getColumnIndex("enable"));
			media_type = Integer.parseInt(mediatypestr);
			enable = Integer.parseInt(enablestr);
		}
		if(enable > 0)
		  return media_type + Constants.BASE_TYPE;
		
		return media_type;
	}
	
	public static boolean isWavesApp(Context mContext){
		boolean isWaves = false;
		ContentResolver contentResolver = mContext.getContentResolver();
		Cursor cursor = contentResolver.query(Constants.CONFIG_URI, new String[]{"media_type"}, null, null, null);
		if(cursor!=null){
			isWaves = true;
		}
		return isWaves;
	}
	
	public static void setAudioTypeToWaves(Context mContext,int mState){
		Log.i("MainActivity","setAudioTypeToWaves...state = "+mState);
		ContentResolver contentResolver = mContext.getContentResolver();
		ContentValues values = new ContentValues();
		boolean isDisable = mState == 3;
		if(isDisable){
		    values.put(Constants.COLUMN_NAME_ENABLE, 0);
		}else{
		    values.put(Constants.COLUMN_NAME_ENABLE, 1);
		    values.put(Constants.COLUMN_NAME_MEDIA_TYPE, mState);
		}
		try {
		   contentResolver.update(Constants.CONFIG_URI, values, null, null);
		} catch (IllegalArgumentException e) {
		}
	}
	
	public static int queryAudioTypeFromDolby(DsGlobalEx mDsClient){
		int profile = -1;
		if(null!=mDsClient){
			if(mDsClient.getState()==1)
				profile = mDsClient.getProfile();//music:1,video:0,systemAudio:2
		}
		return profile + Constants.BASE_TYPE;
	}
	
	public static void setAudioTypeToDolby(DsGlobalEx mDsClient,int profile){
		Log.i("MainActivity","setAudioTypeToDolby...setProfile:"+profile);
		if(mDsClient!=null){
	          try {
				int result = mDsClient.setProfile(profile);
				Log.i("MainActivity", "setProfile:"+profile+" ,result="+result);
			} catch (DsAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public static boolean isDolbyApp(DsGlobalEx mDsClient){
		boolean isDolby = false;
		if(mDsClient!=null){
			String version = mDsClient.getDsVersion();
			if(version!=null&&version.indexOf("DAX")>=0){
				isDolby = true;
			}
		}
		return isDolby;
	}
	
	// smart engine to switch media type by the broadcasted media type value
		public static void smartEngineToSwitchMediaTypeForWaves(Context context,
				int phonemediatype, String media_type, int type_value) {
			// phonemediatype not as type_value,because it will be modified by
			// manual during listening to music
			Log.i("MainActivity","smartEngineToSwitchMediaTypeForWaves...");
			if (media_type != null && (phonemediatype != type_value)
					&& (phonemediatype >= Constants.BASE_TYPE)) {
				//Toast.makeText(context, "WavesMediaType:: " + media_type,
				//		Toast.LENGTH_LONG).show();
				Log.i("MainActivity","pass ...");
				if (type_value == Constants.WAVES_AUDIO_TYPE) {// 音樂
					AudioTypeSetUtils.setAudioTypeToWaves(context, 0);
				} else if (type_value == Constants.WAVES_VIDEO_TYPE) {// 電影
					AudioTypeSetUtils.setAudioTypeToWaves(context, 1);
				} else if (type_value == Constants.WAVES_SYSTEM_AUDIO_TYPE) {// 系統音
					AudioTypeSetUtils.setAudioTypeToWaves(context, 2);
				}
				//NotificationService notifyService = new NotificationService(context);
				//notifyService.simpleNotify("WavesMediaType", media_type);
				Log.i("MainActivity","ForWaves success!!! ...");
			}
		}

		public static void smartEngineToSwitchMediaTypeForDolby(Context context,DsGlobalEx mDsClient, int phonemediatype, String media_type,
				int type_value) {
			//Log.d("MainActivity","internal version:"+mDsClient.getInternalVersion()+",API version:"+mDsClient.getApiVersion()+",DsVersion:"+mDsClient.getDsVersion());
			Log.i("MainActivity", "smartEngineToSwitchMediaTypeForDolby....type_value = "+type_value+"phonemediatype = "+phonemediatype);
			if (mDsClient==null){
				Log.i("MainActivity", "mDsClient==null");
			}
			if (mDsClient!=null&&media_type != null && (phonemediatype != type_value)
					&& (phonemediatype >= Constants.BASE_TYPE)) {
				//Toast.makeText(context, "DolbyMediaType:: " + media_type,
				//		Toast.LENGTH_LONG).show();
				if (type_value == Constants.DOLBY_AUDIO_TYPE) {// 音樂
					AudioTypeSetUtils.setAudioTypeToDolby(mDsClient, 1);
				} else if (type_value == Constants.DOLBY_VIDEO_TYPE) {// 電影
					AudioTypeSetUtils.setAudioTypeToDolby(mDsClient, 0);
				} else if (type_value == Constants.DOLBY_SYSTEM_AUDIO_TYPE) {// 系統音
					AudioTypeSetUtils.setAudioTypeToDolby(mDsClient, 2);
				}
				//NotificationService notifyService = new NotificationService(context);
				//notifyService.simpleNotify("DolbyMediaType", media_type);
				Log.i("MainActivity","ForDolby success!!! ...");
			}
		}
		
}
